package liuds.music.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import liuds.music.R;
import liuds.music.adapter.DropSelectAdapter;

/**
 * Created by Administrator on 2018/6/14.
 */

public class CellView extends LinearLayout {
    private Context context;

    private View rootView;

    private ImageView icon;

    private ImageView link;

    private TextView title;

    private TextView value;

    private View divider;

    public CellView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public CellView(Context context,int icon,String title,String value,boolean linkVisible){
        super(context);
        this.context = context;
        initView();

        setIcon(icon);
        setTitle(title);
        setValue(value);
        setLinkVisible(linkVisible);
    }

    public CellView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        initView();

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CellView);

        if(attributes!=null){

            int icon = attributes.getResourceId(R.styleable.CellView_icon,-1);
            if(icon!=-1){
                setIcon(icon);
            }

            String title = attributes.getString(R.styleable.CellView_title);
            if(title!=null){
                setTitle(title);
            }

            String value = attributes.getString(R.styleable.CellView_value);
            if(value!=null){
                setValue(value);
            }

            int valueColor = attributes.getColor(R.styleable.CellView_valueColor,-1);
            if(valueColor!=-1){
                setValueColor(valueColor);
            }

            Boolean linkVisible = attributes.getBoolean(R.styleable.CellView_link,false);
            setLinkVisible(linkVisible);

            boolean showDivider = attributes.getBoolean(R.styleable.CellView_showDivider,true);
            setShowDivider(showDivider);

            Boolean enabled = attributes.getBoolean(R.styleable.CellView_enabled,true);
            setEnabled(enabled);

            setSingleLine(true);
            attributes.recycle();
        }
    }

    private void initView() {
        rootView = LayoutInflater.from(context).inflate(R.layout.cell_view,this);
        icon = rootView.findViewById(R.id.icon);
        title = rootView.findViewById(R.id.title);
        value = rootView.findViewById(R.id.desc);
        link = rootView.findViewById(R.id.link);
        divider = findViewById(R.id.dividers);
    }

    public void setIcon(int icon) {
        this.icon.setImageResource(icon);
        this.icon.setVisibility(VISIBLE);
    }

    public void setIcon(Drawable icon) {
        this.icon.setImageDrawable(icon);
        this.icon.setVisibility(VISIBLE);
    }

    public void setIcon(Bitmap icon) {
        this.icon.setImageBitmap(icon);
        this.icon.setVisibility(VISIBLE);
    }

    private View rootWindowView;
    private TextView windowTitle;
    private RecyclerView selectListView;
    private DropSelectAdapter dropSelectAdapter;
    //    private PopupWindow dropWindow;
    private BottomSheetDialog dropWindow;
    private Activity activity;

    public void setOptions(Activity activity,DropSelectAdapter dropSelectAdapter, String title){
        setOptions(activity,dropSelectAdapter,title,null);
    }

    public void setOptions(final Activity activity,DropSelectAdapter dropSelectAdapter, String title,String defaultSelectedValue){
        if(dropWindow == null){
            this.activity = activity;
            rootWindowView = LayoutInflater.from(getContext()).inflate(R.layout.drop_select_window,null);

            windowTitle = rootWindowView.findViewById(R.id.windowTitle);
            windowTitle.setText(title);

            selectListView = rootWindowView.findViewById(R.id.selectListView);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
            selectListView.setLayoutManager(layoutManager);
            this.dropSelectAdapter = dropSelectAdapter;
            dropSelectAdapter.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dropWindow.dismiss();
                    int positon = selectListView.getChildLayoutPosition(v);
                    if(CellView.this.dropSelectAdapter.getSelectedIndex() != positon){
                        setSelection(positon);
                    }else {
                        clear();
                    }
                }
            });
            selectListView.setAdapter(dropSelectAdapter);

            rootView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(dropWindow!=null && CellView.this.dropSelectAdapter.getList().size()>0){
//                        dropWindow.showAtLocation(activity.getWindow().getDecorView().getRootView() , Gravity.BOTTOM,0,0);
                        dropWindow.show();
//                        if(changeWindowAlpha){
//                            Utils.changeWindowAlpha(activity,300,1f,0.4f);
//                        }
                    }
                }
            });

            initDropWindow();
            if(defaultSelectedValue != null){
                setSelectionByValue(defaultSelectedValue);
            }
        }
        else{
            dropSelectAdapter.notifyDataSetChanged();
//            setHeight();
        }
    }

    private void initDropWindow() {
//        dropWindow = new PopupWindow(rootWindowView);
//        dropWindow.setFocusable(true);
//        dropWindow.setOutsideTouchable(true);
//        dropWindow.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.drop_select_bg));
//        dropWindow.setAnimationStyle(R.style.dropSelectAnim);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            dropWindow.setElevation(10);
//        }
//
//        dropWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                if(changeWindowAlpha){
//                    Utils.changeWindowAlpha(activity,300,0.4f,1f);
//                }
//            }
//        });
//
//        dropWindow.setWidth(App.screenWidth);
//        setHeight();
        dropWindow = new BottomSheetDialog(activity);
        dropWindow.setContentView(rootWindowView);
        dropWindow.setCanceledOnTouchOutside(true);
    }

    private void setHeight(){
//        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
//        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
//        rootWindowView.measure(w, h);
//
//        BigDecimal bd=new BigDecimal((App.screenHeight/2.5)).setScale(0, BigDecimal.ROUND_HALF_UP);
//        int maxHeight = Integer.parseInt(bd.toString());
//        int windowHeight = rootWindowView.getMeasuredHeight()>maxHeight?maxHeight:rootWindowView.getMeasuredHeight();
//        dropWindow.setHeight(windowHeight);
    }



    private void setValueColor(int color) {
        this.value.setTextColor(color);
    }

    public ImageView getIcon(){
        return this.icon;
    }

    public TextView getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public void setSelectionByValue(String value){
        for(int i=0;i<dropSelectAdapter.getItemCount();i++){
            if(dropSelectAdapter.getTitle(dropSelectAdapter.getList().get(i),i).equals(value)){
                setSelection(i);
                break;
            }
        }
    }

//    public void setSelectionByKey(Object key){
//        for(int i=0;i<dropSelectAdapter.getItemCount();i++){
//            SelectItem selectItem = dropSelectAdapter.getList().get(i);
//            if(selectItem.getKey().equals(key)){
//                setSelection(i);
//                break;
//            }
//        }
//    }

    public void setSelection(int index){
        setValue(dropSelectAdapter.getTitle(dropSelectAdapter.getList().get(index),index));
        dropSelectAdapter.setSelected(index);
    }

    public void setEnabled(boolean enabled){
        if(enabled){
            setClickable(true);
            LinearLayout wrap = rootView.findViewById(R.id.wrap);
            int bottom = wrap.getPaddingBottom();
            int top = wrap.getPaddingTop();
            int right = wrap.getPaddingRight();
            int left = wrap.getPaddingLeft();

            TypedValue typedValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);
            wrap.setBackgroundResource(typedValue.resourceId);
            wrap.setPadding(left,top,right,bottom);
        }else{
            rootView.setOnClickListener(null);
            setClickable(false);

        }
    }

    public void setSingleLine(boolean singleLine){
        if(singleLine){
            value.setGravity(Gravity.END|Gravity.CENTER_VERTICAL);
            value.setSingleLine(true);
        }else{
            value.setGravity(Gravity.START|Gravity.CENTER_VERTICAL);
            value.setSingleLine(false);
        }
    }

    public void setWindowTitle(String windowTitle) {
        this.windowTitle.setText(windowTitle);
    }

    public String getValue() {
        return value.getText().toString();
    }

    public TextView getValueText(){
        return value;
    }

    public void setValue(String value) {
        this.value.setText(value);
    }

    public void setLinkVisible(boolean visible) {
        this.link.setVisibility(visible?VISIBLE:GONE);
    }

    public void setShowDivider(boolean showDividers) {
        divider.setVisibility(showDividers?VISIBLE:GONE);
    }


    public void clear(){
        value.setText("");
        if(dropSelectAdapter != null){
            dropSelectAdapter.clearSelected();
        }
    }

    public void clearOptions(){
        if(dropSelectAdapter != null){
            dropSelectAdapter.getList().clear();
            dropSelectAdapter.notifyDataSetChanged();
            dropSelectAdapter.clearSelected();
        }
    }
}
